package com.xky.example_student.controller.student;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xky.example_student.annotation.RequirePermission;
import com.xky.example_student.entity.Result;
import com.xky.example_student.entity.Student;
import com.xky.example_student.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/student")
@Slf4j
@Tag(name="学生信息管理")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 保存学生信息
     *
     * @param student
     * @return
     */
    @PostMapping("/save")
    @Operation(summary = "保存学生信息")
    public Result<String> saveStudent(@RequestBody Student student){
        log.info("保存学生为{}的信息",student);
        studentService.saveTime(student);
        boolean save = studentService.saveWithStuIdCheck(student);
       if (save){
           return Result.success("保存成功");
       }else {
           return Result.error("学号重复，请检查");
       }
    }

    /**
     * 删除学生id为xx的信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/removeById")
    @Operation(summary = "删除学生信息")
    public Result<String> removeStudentById(Integer id){
        log.info("删除id为{}的学生信息",id);
        boolean b = studentService.removeById(id);
       if (b){
           return Result.success("删除成功！");
       }else {
           return Result.error("该id不存在，删除失败");
       }
    }

    /**
     * 批量删除学生信息
     * @param ids
     * @return
     */
    @DeleteMapping("/removeByIds")
    @Operation(summary = "批量删除学生信息")
    public Result removeStubentByIds(@RequestParam("ids") List<Integer> ids){
        log.info("批量删除学生id为{}的信息",ids);
        studentService.removeByIds(ids);
        return Result.success();
    }

    /**
     * 全删除学生信息
     * @return
     */
    @DeleteMapping("/removeAll")
    @Operation(summary = "全删除学生信息")
    public Result removeStudentAll(){
        log.info("全部删除学生信息");
        boolean isSuccess = studentService.removeAll();
        return isSuccess ? Result.success() : Result.error("删除失败");
    }


    /**
     * 更新学生信息
     *
     * @param student
     * @return
     */
    @PostMapping("/update")
    @Operation(summary = "更新学生信息")
    @RequirePermission(value = "studentEdit", message = "您暂时没有修改学生信息的权限")
    public Result<String> updateStudent(@RequestBody Student student){
        try {
            log.info("更新学生信息");
            boolean b = studentService.updateByIdSa(student);
            if (b){
                return Result.success("该学生信息更新成功！");
            }else {
                return Result.error("该学生信息更新失败！");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据id查询学生信息
     *
     * @param id
     * @return
     */
    @GetMapping("/listById")
    @Operation(summary = "查询id为xx的学生信息")
    public Result<List<Student>> listStudentById(@RequestParam("id") Integer id){
        log.info("根据id查询id为{}学生信息",id);
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("id",id);
        List<Student> list = studentService.list(studentQueryWrapper);

        if (list.isEmpty()){
            return Result.error("没有该学号");
        }else {
            return Result.success(list);
        }
    }

    /**
     * 查询全部的学生信息
     *
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "查询全部的学生信息")
    public Result<List<Student>> listAll(){
        log.info("查询全部的学生信息");
        // 使用不包含status字段的自定义查询方法
        List<Student> list = studentService.listAllStudents();

        if (list.isEmpty()){
            return Result.error("没有查询到学生信息");
        }else {
            return Result.success(list);
        }
    }


    /**
     * 分页查询学生信息
     *
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/list/page")
    @Operation(summary = "分页查询学生信息")
    public Result<Page<Student>> pageStudent(@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "5") Integer size){
        log.info("分页查询学生信息");
        Page<Student> studentPage = new Page<>(current,size);
        // 使用不包含status字段的自定义分页查询方法
        Page<Student> page = studentService.pageStudents(studentPage);
        return Result.success(page);
    }



}



